<template>
  <div>
    <Breadcrumbs :breadtitle="{ '1': '商品管理', '2': '商品分类' }"></Breadcrumbs>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加分类 -->
      <el-row :gutter="20" type="flex" justify="end">
        <el-col :span="2" :pull="1">
          <el-button @click="diaAddSetCate=true;AddSetCateData=null">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table :data="CategoriesList" :columns="columns" :selection-type="false" :expand-type="false" v-if="CategoriesList" show-index border style="margin:15px 0 15px 0">
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="!scope.row.cat_deleted" style="color:lightgreen;fontSize:15px"></i>
          <i class="el-icon-error" v-else style="color:red;fontSize:15px"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="medium" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag type="success" size="medium" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag type="warning" size="medium" v-else-if="scope.row.cat_level===2">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="optbtn" slot-scope="scope">
          <el-button size="mini" type="primary" @click="diaAddSetCate=true;AddSetCateData=null;AddSetCateData=scope.row;">编辑</el-button>
          <el-button size="mini" type="danger" @click="BtnDelCategories(scope.row)">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination :page-sizes="[1, 2, 5, 7]" :page-size="queryInfo.pagesize" :current-page="queryInfo.pagenum" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange">
      </el-pagination>
    </el-card>

    <AddSetCategories @OutdiaAddSet="OutdiaAddSet" @getCategories="getCategories" :diaAddSetCate="diaAddSetCate" :AddSetCateData="AddSetCateData"></AddSetCategories>
  </div>
</template>

<script>
import Breadcrumbs from '@/components/Breadcrumbs'
import AddSetCategories from '@/components/Goods/AddSetCategories'

export default {
  name: "Categories",
  data() {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1, //当前页数
        pagesize: 7, //每页显示多少条数据
      },
      total: null,
      CategoriesList: null,
      AddSetCateData: null,
       diaAddSetCate: false,
      columns: [
        { label: '分配名称', prop: 'cat_name' },
        { label: '是否有效', prop: 'roleDesc', type: 'template', template: 'isok' },
        { label: '排序', prop: 'roleDesc', type: 'template', template: 'order' },
        { label: '操作', prop: 'roleDesc', type: 'template', template: 'optbtn' },
      ],
    }
  },
  components: {
    Breadcrumbs,
    AddSetCategories
  },
  created() {
    this.getCategories() //调用ajax商品分类
  },
  methods: {
    getCategories() {  //获取商品分类
      this.$http.get('categories', { params: this.queryInfo }).then(res => {
        if (res.data.meta.status !== 200) return
        this.CategoriesList = res.data.data.result
        this.total = res.data.data.total
      })
    },
    handleSizeChange(newSize) {   //监听pagesize改变事件
      this.queryInfo.pagesize = newSize
      this.getCategories();
    },
    handleCurrentChange(newNum) {   //监听 页码值 改变事件
      this.queryInfo.pagenum = newNum
      this.getCategories();
    },
    BtnDelCategories(item) {  //删除分类
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$http.delete(`categories/${item.cat_id}`).then(res => {
          if (res.data.meta.status !== 200) {
            return this.$mess(res.data.meta.msg, 'error')
          }
          this.$mess(res.data.meta.msg, 'success')
          this.getCategories()
        })
      }).catch(() => {
        this.$mess('已取消删除', 'info')
      })
    },
    OutdiaAddSet() {  //关闭弹窗
      this.diaAddSetCate = false
    }
  }
}
</script>

<style>
</style>